Import mojo.app

Import mojo.graphics
Import mojo.input
Import mojo.audio
Import tween2
Import diddy
Import model
Import numberButtonsBonus1
'
' NumberApp 
'

Global LilGuyList:List<LilGuy> = New List<LilGuy>
Global ScoreBubbleList:List<ScoreBubble> = New List<ScoreBubble>


	Global PLAYING_AREA_HEIGHT:Int
	Global  PLAYING_AREA_WIDTH:Int	
Class BonusShootScreen Extends Screen

	Const ADD_MODE = 1
	Const SUBTRACT_MODE = 2
	Const MULTIPLY_MODE = 4
	Const DIVIDE_MODE = 8

	Const START_GAME_STATE = 0
	Const BETWEEN_LEVEL_STATE = 1
	Const PLAYING_GAME_STATE = 2
	Const GAME_OVER_STATE = 3 
	Const CREATE_STATE = 4
	
	Const GRAVITY:Float = 0.3
	
	Const SCORE_STAR:Int = 100
	
	Const SCORE_TIME_LEFT:Int = 10
	
	Const MULTI_CHOICE_PANEL_WIDTH:Int =150
	

	
	Field baddyNumber1:BaddyNumber
	Field baddyNumber2:BaddyNumber
	Field baddyNumber3:BaddySign
	Field bulletNumber1:Bullet
	Field bulletNumber2:Bullet
	Field bulletNumber3:Bullet
	Field player:PlayerBonus

	Field text:String="INPUT:"
	Field score:Int=0
	Field modeSymbol:String = "+"
	
	
	'Field mathMode:Int=ADD_MODE
	Field numberLimitMin:Int=1
	Field numberLimitMax:Int=10
	Field multiplyLimitMax:Int=13
	Field divideLimitMax:Int=11
	Field mathMode:Int = BonusShootScreen.ADD_MODE
	Field mathModeThisGo = BonusShootScreen.ADD_MODE
	Field startVelocity:Float
	Field velocityIncrement:Float
	
	Field collisionWidth = 66
	Field collisionHeight = 49
	Field gameOver:Bool = False
	Field fart1:Sound
	Field fart2:Sound
	Field spark:GameImage
	Field FPS% = 60
	Field ballonsThisLevel:Int = 0
	Field pointsToMax:Float = 12 ' Best if this is divisible by 3
	
	Field gameState:Int = CREATE_STATE
	Field nextGameState:Int = START_GAME_STATE
	
	Field realMilliSecs1:Int
	Field realMilliSecs2:Int
	Field timer:Int = 0
	Field getReadyX:Float=0
	Field endLevelFadeTween:Tween2= New Tween2(Tween2.Cubic.EaseInOut,1,0,2000)
	Field newLevelFadeTween:Tween2= New Tween2(Tween2.Cubic.EaseInOut,0,1,1000)
	
	Field getReadyTween1:Tween2= New Tween2(Tween2.Elastic.EaseInOut,SCREEN_WIDTH+100,SCREEN_WIDTH2,1000)
	Field getReadyTween2:Tween2= New Tween2(Tween2.Elastic.EaseInOut,SCREEN_WIDTH2,-100,1000)
	Field currentGetReadyTween:Int=0
	Field timeLastStateChanged:Int = 0
	Field timeSinceLastStateChange:Int = 0
	
	Field betweenLevelTimer:Int
	
'	Field gameMode:Int
	
	Field levelComplete:bool = false
	
	Field multiChoicePanel:MultiChoicePanel
	
	Field scoreStatusBar:ScoreStatusBar
	Field bgImage:GameImage
	Field answer:Int

	Method New()
	
	Print ("NEW BONUS SHOOT SCREEN")
	
		name = "Bonus"
		
		' Store the device width and height
	
        PLAYING_AREA_HEIGHT =  SCREEN_HEIGHT-100
		PLAYING_AREA_WIDTH =   SCREEN_WIDTH-MULTI_CHOICE_PANEL_WIDTH
		SetUpdateRate FPS
		ballonsThisLevel = 0
	
		spark = game.images.Find("glow-1")
		bgImage = game.images.Find("bonusShootBG")
		
		player = New PlayerBonus(100, SCREEN_HEIGHT-50)
		multiChoicePanel = New MultiChoicePanel(SCREEN_WIDTH-MULTI_CHOICE_PANEL_WIDTH,0, MULTI_CHOICE_PANEL_WIDTH,SCREEN_HEIGHT, 4)
		scoreStatusBar = New ScoreStatusBar(multiChoicePanel.x-40, 10, SCREEN_HEIGHT-20, pointsToMax)
		
		baddyNumber1 = New BaddyNumber()
		baddyNumber2 = New BaddyNumber()
		baddyNumber3 = New BaddySign()
	End
	
	Method Start:Void()

		LilGuyList.Clear
		ScoreBubbleList.Clear
		Particle.Clear
		
		scoreStatusBar.Reset()
		
		
		bulletNumber1 = New Bullet
		bulletNumber2 = New Bullet
		bulletNumber3 = New Bullet
	
		Seed = Millisecs()
		timer = 60
		
			if(_model.currentDifficulty=Model.DIFFICULTY_EASY)
			mathMode=ADD_MODE + SUBTRACT_MODE 
			startVelocity=1
			velocityIncrement=0
			numberLimitMin=1
			numberLimitMax=10
			Print "DIFFICULTY_EASY"
		ElseIf(_model.currentDifficulty=Model.DIFFICULTY_MEDIUM)
mathMode=ADD_MODE + SUBTRACT_MODE 
			startVelocity=1
			velocityIncrement=0
			numberLimitMin=1
			numberLimitMax=50
				Print "DIFFICULTY_MEDIUM"
		ElseIf(_model.currentDifficulty=Model.DIFFICULTY_HARD)
mathMode=ADD_MODE + SUBTRACT_MODE + MULTIPLY_MODE + DIVIDE_MODE
			startVelocity=1.1
			velocityIncrement=0.1
			numberLimitMin=1
			numberLimitMax=100
			multiplyLimitMax=13
			divideLimitMax=10
				Print "DIFFICULTY_HARD"
				ElseIf(_model.currentDifficulty=Model.DIFFICULTY_GENIUS)
mathMode=ADD_MODE + SUBTRACT_MODE + MULTIPLY_MODE + DIVIDE_MODE 
			startVelocity=1.5
			velocityIncrement=0.1
			numberLimitMin=1
			numberLimitMax=100
			multiplyLimitMax=13
			divideLimitMax=10
				Print "DIFFICULTY_GENIUS"
		EndIf
		
		
		GenerateBaddys()
		player.x = SCREEN_WIDTH2
		player.y=SCREEN_HEIGHT-50
		player.dx=-1
		
		
		baddyNumber1.dy = startVelocity
		baddyNumber2.dy = startVelocity
		baddyNumber3.dy = startVelocity
		
		ChangeGameState(START_GAME_STATE)
		
    End

    Method OnLoading:Int()
        Return 0
    End

	
    Method Update:Void()
		Local hasInput = False
		
		'
		' Update state change, state changes are saved for the next update
		'
		UpdateState()
		
		If KeyHit(KEY_ESCAPE)
			game.screenFade.Start(50, True)
			game.nextScreen = _model._mainMenuScreen			
		End
		
		_model.messageWindow.Update()

		multiChoicePanel.Update
		
		Select gameState
		Case START_GAME_STATE

		Case BETWEEN_LEVEL_STATE
			player.Update()
		
			if(currentGetReadyTween=1)
				getReadyTween1.Update()
				getReadyX=getReadyTween1.Value()
			elseif(currentGetReadyTween=2)
				getReadyTween2.Update()
				getReadyX=getReadyTween2.Value()
			End
			
			if(getReadyTween1.isActive=false and currentGetReadyTween=1)
				currentGetReadyTween=2
				getReadyTween2.Start()
			End
			
			if((newLevelFadeTween.isActive=false and TouchHit()) or timeSinceLastStateChange>3000)
				ChangeGameState(PLAYING_GAME_STATE)
			Endif

		Case PLAYING_GAME_STATE
			baddyNumber1.OnUpdate()
			baddyNumber2.OnUpdate()
			baddyNumber3.OnUpdate()
			bulletNumber1.OnUpdate()
			bulletNumber2.OnUpdate()
			bulletNumber3.OnUpdate()
	
			'
			' Player
			'
			player.Update()
			'
			' LilGuys
			'
			UpdateLilGuysZ()
			
			'
		' Multiple Choice buttons
		'
		multiChoicePanel.Update()

		if(multiChoicePanel.pressedNumber<>MultiChoicePanel.NO_ANSWER)
			if(multiChoicePanel.pressedNumber=answer)
				'multiChoicePanel.NewQuestion(answer)	
				scoreStatusBar.AddPoints()
				
				bulletNumber1.x=player.x
				bulletNumber1.y=player.y
				bulletNumber1.FindPath(baddyNumber1.x, baddyNumber1.y, baddyNumber1.dy+6)
				bulletNumber1.kill=False
				
				bulletNumber2.x=player.x
				bulletNumber2.y=player.y
				bulletNumber2.FindPath(baddyNumber2.x, baddyNumber2.y, baddyNumber2.dy+6)
				bulletNumber2.kill=False
				
				bulletNumber3.x=player.x
				bulletNumber3.y=player.y
				bulletNumber3.FindPath(baddyNumber3.x, baddyNumber3.y, baddyNumber3.dy+6)
				bulletNumber3.kill=False
				
				text = multiChoicePanel.answer
				hasInput = True
			Else 
				scoreStatusBar.SubtractPoints
			EndIf
			multiChoicePanel.pressedNumber=MultiChoicePanel.NO_ANSWER
		End
		
'			Local char=GetChar()
'			If(char>=32)
'				text=Int(char)-48
'				hasInput = True
'			Else If (numberButton.inputNumber<>"")
'				' get number pressed by mouse:
'				text = numberButton.inputNumber
'				hasInput = True
'				numberButton.Clear()
'			EndIf
			

			
			If (baddyNumber1.kill = True And baddyNumber2.kill = True And baddyNumber3.kill = True)
				score=score+1
				ballonsThisLevel+=1
				scoreStatusBar.AddPoints()
				
			'	If(ballonsThisLevel>level.MaxBallonsThisLevel)
			'		currentState=BETWEEN_LEVEL_STATE
			'		_model.messageWindow.Show("YOU GOT x STARS", new EndGameMessageCallback2())
			'	Endif
				GenerateBaddys()
			Endif
			
			If ((baddyNumber1.y> PLAYING_AREA_HEIGHT) And 
			    (baddyNumber2.y> PLAYING_AREA_HEIGHT) And 
			    (baddyNumber3.y> PLAYING_AREA_HEIGHT))
				'GenerateBaddys()
				'gameState=GAME_OVER_STATE
			'	gameOver=True
			'	_model.bonusGameFailed = true
			'	_model.bonusGameSuccess=false
			'	game.screenFade.Start(50, True)
			'	game.nextScreen = _model._mazeScreen
				scoreStatusBar.SubtractPoints()
				GenerateBaddys()
			Endif
			
			checkCollisions()
			Particle.UpdateAll()
			UpdateLilGuysZ()
			UpdateScoreBubblesZ()
				
			'
			' Check for game over
			'
			if (scoreStatusBar.GetSelectedStars() = scoreStatusBar.STARS_MAX)
				ChangeGameState(GAME_OVER_STATE)			
			elseif (timer<=0)
				ChangeGameState(GAME_OVER_STATE)
			endif
			
			'
			' Work out when 1 second has elapsed and update timer
			'
			realMilliSecs2=RealMillisecs()
			if(realMilliSecs2>realMilliSecs1+1000)
				timer-=1
				realMilliSecs1=RealMillisecs()
			endif
		Case GAME_OVER_STATE
		End
			
		'
		' status bars
		'
		scoreStatusBar.Update()
    End

    Method Render:Void()
		SetAlpha 1
		Cls 0,0,0
		
		bgImage.Draw(SCREEN_WIDTH2, SCREEN_HEIGHT2)
		
		
		
		' 
		' Draw LilGuys
		'
		DrawLilGuysZ()
		
		SetColor(255,255,255)
		
		Select gameState			
			Case START_GAME_STATE
				
			Case BETWEEN_LEVEL_STATE
				_model.font.DrawText("GET READY!", getReadyX, SCREEN_HEIGHT2+30, AngelFont.ALIGN_CENTER)
			Case PLAYING_GAME_STATE				
				baddyNumber1.Draw()
				baddyNumber2.Draw()
				baddyNumber3.Draw()

				bulletNumber1.Draw()
				bulletNumber2.Draw()
				bulletNumber3.Draw()

				
				Particle.DrawAll()
						
			Case GAME_OVER_STATE
		
		End
		
		'
		' Player
		'
		player.Draw()
		
		'
		' Multiple Choice buttons
		'
		multiChoicePanel.Render()
		
		
		SetAlpha 1
		
		_model.fpsCounter.Draw(100,0)
		
		'
		' status bars
		'
		scoreStatusBar.Draw()
		
		DrawText ("TIME: "+timer, 10,10)
		
		_model.messageWindow.Draw()
    End    

	Method GenerateBaddys:Void()
		baddyNumber1.x = Rnd(50, (PLAYING_AREA_WIDTH/2)-50)
		baddyNumber2.x = Rnd((PLAYING_AREA_WIDTH/2)+50, (PLAYING_AREA_WIDTH)-50)
		baddyNumber3.x =( PLAYING_AREA_WIDTH)/2

		baddyNumber1.y =-50
		baddyNumber2.y =-50
		baddyNumber3.y =-50
		
		bulletNumber1.kill=True
		bulletNumber2.kill=True
		bulletNumber3.kill=True
		
		baddyNumber1.kill=False
		baddyNumber2.kill=False
		baddyNumber3.kill=False
		
		baddyNumber1.dy+=velocityIncrement
		baddyNumber2.dy+=velocityIncrement
		baddyNumber3.dy+=velocityIncrement
		
		'too close
		While(Abs(baddyNumber2.x-baddyNumber1.x) <60)
			baddyNumber2.x += 10
			baddyNumber1.x -= 10
		Wend
		
		' Work out sign to display based on level preferences
		Local gotMode:Bool=false
		
		' Repeat until we get a mode that is ok for this difficulty
		while(gotMode=False)
			mathModeThisGo = Rnd(1,9)
			
			if(mathMode & mathModeThisGo >0)
				gotMode=true
			EndIf
		Wend


		if(mathModeThisGo & ADD_MODE = ADD_MODE)
				baddyNumber1.number=Rnd(numberLimitMin,numberLimitMax)
				baddyNumber2.number=Rnd(numberLimitMax-baddyNumber1.number)
				modeSymbol = "+"
				answer=baddyNumber1.number+baddyNumber2.number
		ElseIf(mathModeThisGo & SUBTRACT_MODE = SUBTRACT_MODE)
				baddyNumber1.number=Rnd(numberLimitMin,numberLimitMax)
				baddyNumber2.number=Rnd(0, baddyNumber1.number)
				modeSymbol = "-"
				answer=baddyNumber1.number-baddyNumber2.number
		ElseIf(mathModeThisGo & MULTIPLY_MODE = MULTIPLY_MODE)
				baddyNumber1.number=Rnd(0,multiplyLimitMax)
				baddyNumber2.number=Rnd(0, baddyNumber1.number)
				modeSymbol = "X"
				answer=baddyNumber1.number*baddyNumber2.number	
		ElseIf(mathModeThisGo & DIVIDE_MODE = DIVIDE_MODE)
			
				Local firstNumber:Int = Rnd(1,divideLimitMax)
				Local answer2:Int = Rnd(1,divideLimitMax)
				Local secondNumber = answer2 * firstNumber
				
				baddyNumber2.number=firstNumber
				baddyNumber1.number=secondNumber
				modeSymbol = "/"
				answer=baddyNumber1.number/baddyNumber2.number									
		Endif

		
		baddyNumber3.sign = modeSymbol
		
		multiChoicePanel.NewQuestion(answer)
	End
	
	Method checkCollisions:Void()
	If(bulletNumber1.kill=False) Then
		If(bulletNumber1.y<baddyNumber1.y+(baddyNumber1.radius/2)) 
			bulletNumber1.kill=True
			baddyNumber1.collision=True
			For Local i% = 1 To 6
					Particle.Create(spark, bulletNumber1.x, bulletNumber1.y, Rnd(-2,2), Rnd(-3,-1), GRAVITY/4, 2000)
			Next
			_model.addStar()
			'ParticleExplosion(bulletNumber1.x, bulletNumber1.y, particle, 30, 100)
			Local lilGuy:LilGuy = New LilGuy(bulletNumber1.x, bulletNumber1.y)
		'	PlaySound( fart1, 0, 0)
		Endif
	Endif
	If(bulletNumber2.kill=False) Then
		If(bulletNumber2.y<baddyNumber2.y+(baddyNumber2.radius/2)) 
			bulletNumber2.kill=True
			baddyNumber2.collision=True
			For Local i% = 1 To 6
				Particle.Create(spark, bulletNumber2.x, bulletNumber2.y, Rnd(-2,2), Rnd(-3,-1), GRAVITY/4, 2000)
			Next
			Local lilGuy:LilGuy = New LilGuy(bulletNumber2.x, bulletNumber2.y)
		'	PlaySound( fart2, 0, 0)
		Endif
	Endif
	If(bulletNumber3.kill=False) Then
		If(bulletNumber3.y<baddyNumber3.y+(baddyNumber3.radius/2)) 
			bulletNumber3.kill=True
			baddyNumber3.collision=True
			For Local i% = 1 To 6
			Particle.Create(spark, bulletNumber3.x, bulletNumber3.y, Rnd(-2,2), Rnd(-3,-1), GRAVITY/4, 2000)
			Next
			
			'ParticleExplosion(bulletNumber3.x, bulletNumber3.y, particle, 30, 100)
			Local lilGuy:LilGuy = New LilGuy(bulletNumber3.x, bulletNumber3.y)
			'PlaySound( fart3, 0, 0)
		Endif
	Endif


	End
	
	
	Function FloatArray2D:Float[][] (X%,Y%)
		Local tmp:Float[X][]
		For Local I% = 0 Until X
			tmp[I] = New Float[Y]
		Next
		Return tmp
	
		'Local tmp := [New Float[X],New Float[Y]]  ' >:-(
	End Function
	
	Method UpdateState:void()
		if(nextGameState<>gameState)
			ChangeGameStateNextUpdate(nextGameState)
		End
		
		'
		' Time since state changed
		timeSinceLastStateChange = RealMillisecs()-timeLastStateChanged
	End
	
	Method ChangeGameState:void(newState:Int)
		nextGameState=newState
	End
	
	'***************************
	'
	'
	Method ChangeGameStateNextUpdate:void(newState:Int)
		Print "Changing state from "+gameState+"to : "+newState
		timeLastStateChanged=RealMillisecs()
		gameState=newState	
		Select(gameState)
			Case PLAYING_GAME_STATE
						
			Case BETWEEN_LEVEL_STATE	
				initBeginLevelState()
			Case START_GAME_STATE
				initStartState()
			Case GAME_OVER_STATE
				initGameOverState()
		End
	End
		'***************************
	'
	'
	Method initStartState:void()
		_model.messageWindow.Show("SOLVE THE SUMS;TO ESCAPE THE;LADYBIRDS" , New StartGameMessageCallback(self))
	End
	
	'***************************
	'
	'
	Method initBeginLevelState:void()
		newLevelFadeTween.Start()
		currentGetReadyTween=1
		getReadyTween1.Start()
	End
	
	
	'***************************
	'
	'
	Method initGameOverState:void()
		Local message:String
		Local score:Int
		
		score = scoreStatusBar.GetSelectedStars() * SCORE_STAR
		
		if(timer=0)
			message = "OUT OF TIME;;"
		else
			score += timer*SCORE_TIME_LEFT
			message = "LEVEL CLEARED;;"
		End
		message += "YOU GOT "+scoreStatusBar.GetSelectedStars()+" STARS;;"
		message += "SCORE: "+score
		
		_model.stars += scoreStatusBar.GetSelectedStars()
		_model.score += score
		_model.messageWindow.Show(message, new EndGameMessageCallback())
	End
End
'
'BaddyNumber
'
Class BaddyNumber Extends Sprite
	Field radius:Float
	Field scaledRadius:Float
	Field kill = False
	Field angle:Float
	Field number:Int
	Field collision:Bool = False
	Field collisionAnimCounter:Int = 20
	
	Method New()
		Super.New(game.images.Find("yellowGuy"), 0,0)
		scaleX=2    
		scaleY=2       
		radius = 32
		collisionAnimCounter=20
		angle=Rnd(50)
		collision=False
		kill=False

    End

	Method OnUpdate:Int()
		If (kill=False)
		
			Move()
			angle = angle+1 * dt.delta

		Endif
		If (collision=True)
	
				collision=False
				kill=True				
		
		Endif
        Return 0 

    End

    Method Draw:Void()
		If (kill=False)
			
			PushMatrix 
			Translate (x, y)
			'Scale (scaleX,scaleY)
			'Rotate(Sin(angle)*20)
			
			'SetColor( 255,0,0 )
			
			DrawImage(image.image, 0 , 0)
			SetColor( 255,255,255 )
			Translate (-8, -8 )
			_model.font.DrawText(String(number), 0, 0)
			PopMatrix 
		Endif
    End   
End

'
'BaddySign
'
Class BaddySign Extends BaddyNumber

	Field sign:String
	Field modeSymbol:String
	
	Method New()  
	Super.New()
		x = SCREEN_WIDTH2
    End

    Method Draw:Void()
		If (kill=False)
'			if(sign & BonusShootScreen.ADD_MODE = BonusShootScreen.ADD_MODE)			
'					modeSymbol = "+"
'			elseif(sign & BonusShootScreen.SUBTRACT_MODE = BonusShootScreen.SUBTRACT_MODE)			
'					modeSymbol = "-"
'			End'Select

			PushMatrix 
			Translate (x, y)
		'	Scale (scaleX,scaleY)
		'	Rotate(Sin(angle)*20)
			
			'SetColor( 255,0,0 )
			
			DrawImage(image.image, 0 , 0)
			SetColor( 255,255,255 )
			Translate (-8, -8 )
			_model.font.DrawText(sign , 0, 0)
			PopMatrix 
		Endif
    End   
End

Class LilGuy Extends Sprite
	Const FALLING_STATE:Int = 0
	Const GROUND_STATE:Int = 1
	
	Field destx:Float
	Field desty:Float
	Field pathLen:Int
	Field kill:Bool = False
	Field onGround = False
	Field homeLocation = False
	Field tweenFalling:Tween2
	Field tweenGround:Tween2
	Field state:Int = FALLING_STATE
	
	Method New(_x:Float, _y:Float)   
	Super.New(game.images.Find("alienGuy"),_x, _y)
		 LilGuyList.AddLast(Self)
     
		tweenFalling = New Tween2(Tween2.Cubic.EaseInOut,y,DeviceHeight-50,2000)
		tweenGround = New Tween2(Tween2.Cubic.EaseInOut,x,-50,2000)
        tweenFalling.Start() 	
    End

	Method Update:Int()	
		Select state
			Case FALLING_STATE
				tweenFalling.Update()
				y=tweenFalling.Value()
				
				if(tweenFalling.isActive=False)
					tweenGround.Start()
					state = GROUND_STATE
				EndIf			
		
			Case GROUND_STATE
				tweenGround.Update()			
				x=tweenGround.Value()
		End

        Return 0 
    End

   
End

Function UpdateLilGuysZ()	
	For Local UpdateP:LilGuy = Eachin LilGuy.LilGuyList
			UpdateP.Update()
	Next
End Function

Function ClearLilGuysZ()	
	LilGuy.LilGuyList.Clear()
End Function

Function DrawLilGuysZ()	
For Local UpdateP:LilGuy = Eachin LilGuy.LilGuyList
			UpdateP.Draw(true)
	Next
End Function



Class Bullet Extends Sprite
	Field destx:Float
	Field desty:Float
	Field pathLen:Int
	Field kill:Bool = True
	
	Method New()
	Super.New(game.images.Find("16CircleOrange")   ,0,0)    
		speed=5   	
    End

	Method OnUpdate:Int()	
		If(kill=False) Then
			Move()						
		Endif
        Return 0 
    End

    Method Draw:Void()
		If(kill=False) Then
			Super.Draw(0,0)
		Endif
    End
 
	Method FindPath:Void(baddyX:Int, baddyY:Int, speed:Float)
		'pythagorus
		Local a:Float
		Local b:Float
		Local c:Float
		
		a = baddyX-x
		b = baddyY-y
		
		destx=baddyX
		desty=baddyY
		c = Sqrt(Pow(a,2)+Pow(b,2))
		
		dx= (a/c)*speed
		dy= (b/c)*speed
		
	End
End

Class PlayerBonus Extends Sprite
	Field destx:Float
	Field desty:Float
	Field pathLen:Int
	Field kill:Bool = True

	Method New(x:Float, y:Float)
		Super.New( game.images.Find("mapstrip")  ,x,y )  
		
		destx=x
		desty=y
		speed=1
		
		'FindPath(destx, desty)
		frame=3
    End

	Method Update:Void()	

		UpdateAnimation()
		Move()
		
		If (dx<0 And (x<destx)) Or 
		   (dx>0 And (x>destx))Then
			destx=Rnd(PLAYING_AREA_WIDTH)+50
			FindPath(destx, desty)		
		Endif	
		
		If(destx>x)
			scaleX=-1
		Else
			scaleX=1
		Endif
	
    End

	Method FindPath:Void(baddyX:Int, baddyY:Int)
		'pythagorus
		Local a:Float
		Local b:Float
		Local c:Float
		
		a = baddyX-x
		b = baddyY-y
		
		destx=baddyX
		desty=baddyY
		c = Sqrt(Pow(a,2)+Pow(b,2))
		
		dx= (a/c)*speed
		dy= (b/c)*speed
	End


End

' written after drinking whisky, beware
Class ScoreBubble
	Field tween:Tween2
	Field tweenAlpha:Tween2
	Field x:Float
	Field y:Float
	Field destY:Float
	Field score:String = "+1"
	
	Method Create(_x:Int, _y:Int)
		ScoreBubbleList.AddLast(Self)
		x=_x
		y=_y
		destY=y-250
		tween = New Tween2(Tween2.Linear,y,destY,4000)
		tweenAlpha = New Tween2(Tween2.Linear,1,0,4000)

		tween.Start()
		tweenAlpha.Start()
	End
	
	Method Update()
		If (y<=destY)
			'Kill me
			ScoreBubbleList.Remove(Self)
		End
		tween.Update()
		tweenAlpha.Update()
	End
	
	Method Render()
		y = tween.Value()
		PushMatrix
		SetAlpha(tweenAlpha.Value())
		DrawText (score,x,y,  0.5, 0.5)
		
		PopMatrix
	End
End

Function UpdateScoreBubblesZ()	
	For Local UpdateP:ScoreBubble = Eachin ScoreBubble.ScoreBubbleList
			UpdateP.Update()
	Next
End Function

Function ClearScoreBubblesZ()	
	ScoreBubble.ScoreBubbleList.Clear()
End Function

Function DrawScoreBubblesZ()	
For Local UpdateP:ScoreBubble = Eachin ScoreBubble.ScoreBubbleList
			UpdateP.Render()
	Next
End Function


Class NumberButtonsBonus
	Field width = (DeviceWidth()-PLAYING_AREA_WIDTH)/2
	Field height = PLAYING_AREA_HEIGHT/5
	Field highlightNumber = 0
	Field pressedNumber:int = -1
	
	Method Update()
		Local number:Int = 0
		Local mouseY:Float=MouseY()
		Local mouseX:Float=MouseX()
		pressedNumber = -1
		
		If (mouseX>PLAYING_AREA_WIDTH)
			For Local y:Int = 1 To 5
				For Local x:Int = 1 To 2
					number += 1
					if(mouseX>x*width-width+PLAYING_AREA_WIDTH And mouseX<x*width+PLAYING_AREA_WIDTH And mouseY>y*height-height And mouseY< y*height)
						highlightNumber = number
					End
					
				Next
			Next
		Endif
		
		If (MouseHit(MOUSE_LEFT)>0)
			pressedNumber = highlightNumber
			Print(pressedNumber)			
		Else
			pressedNumber = -1
		End
	End
	
	
	Method Render()
		Local number:Int = 0
		PushMatrix		
		For Local y:Int = 1 To 5
		For Local x:Int = 1 To 2
		
		
		number += 1
		
		If(number=highlightNumber)
			SetColor(0,200,0)
		Else
			SetColor(200,0,0)
		endif
		DrawRect(x*width-width+PLAYING_AREA_WIDTH, y*height-height, width, height )
		DrawText(number, x*width-width/2+PLAYING_AREA_WIDTH, y*height-height/2)

		Next
		Next
		PopMatrix
		
	End
	
End

Class EndGameMessageCallback2 Implements WindowCallback

	Method onClose:Void()
		_model.bonusGameSuccess=true
		_model.bonusGameFailed=false
		game.screenFade.Start(50, True)
		game.nextScreen = _model._mazeScreen
	End
End

Class StartGameMessageCallback Implements WindowCallback
	Field bonusShootScreen:BonusShootScreen
	
	Method New(bonusShootScreen:BonusShootScreen)
		self.bonusShootScreen = bonusShootScreen
	End
	
	Method onClose:Void()
		self.bonusShootScreen.ChangeGameState(bonusShootScreen.BETWEEN_LEVEL_STATE)
	End
End













